package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.enums.AppointmentStatusEnum;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.impl.BusinessServiceImpl;
import cn.wolfcode.service.impl.SystemDictionaryItemServiceImpl;
import cn.wolfcode.util.JsonResult;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private SystemDictionaryItemServiceImpl systemDictionaryItemService;
    @Autowired
    private IBusinessService businessService;

    @RequiredPermission(name = "预约单主页", expression = "appointment:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") AppointmentQueryObject qo) {
        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //过滤查询的预约单状态下拉框回显
        model.addAttribute("appointmentEnums",AppointmentStatusEnum.values());
        //过滤查询的门店查询下拉框回显
        model.addAttribute("businesses",businessService.selectAll());

        return "appointment/list";
    }

    //查询业务大类
    @RequestMapping("/selectByTypeId")
    public List<SystemDictionaryItem> selectByTypeId(Model model, Long id) {
        //所有的业务大类
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.selectByTypeId(id);

        return  systemDictionaryItems;
    }


    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "预约单新增/编辑", expression = "appointment:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Appointment appointment) {
        if (appointment.getId() != null) {
            appointmentService.update(appointment);
        } else {
            appointmentService.save(appointment);
        }
        return new JsonResult();
    }

    @RequestMapping("/updateStatus")
    @RequiredPermission(name = "预约单状态修改", expression = "appointment:updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long id,Integer status) {
        appointmentService.updateStatus(id,status);
        return new JsonResult();
    }


}
